/*
* $Id: JournalSettings.cpp 156 2010-09-28 19:00:52Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "JournalSettings.h"
#include "../SettingControlIds.h"
#include "../DinIntDefined.h"
#include "Settings/Journal/ListBoxStateParser.h"
#include "Components/Journal/HousingManager.h"

#define JOURNAL_REG_NAME	_T("Software\\Apiary\\JournalSettings")

CJournalSettings::CJournalSettings(IJournalComponent* pJournalComponents)
: CSettings(JOURNAL_CAPTION_ID, JOURNAL_REG_NAME, TRUE){
	m_pJournalComponents = pJournalComponents;
	Init();
}
CJournalSettings::~CJournalSettings(){}

std::map<std::tstring, std::vector<Parameter> > CJournalSettings::GetParameters(){
	std::map<std::tstring, std::vector<Parameter> > aMapParameters;
	std::vector<Parameter> aParameters;

	//static_header     
	aParameters.push_back(Parameter(TYPE, HEADER));
	aMapParameters.insert(aMapParameters.end(), std::make_pair(STATIC_HEADER_ID, aParameters));

	//static_style
	aParameters.clear();
	aMapParameters.insert(aMapParameters.end(), std::make_pair(STATIC_STYLE_ID, aParameters));

	//combo_style
	std::vector<std::tstring> aStyles;
	if(m_pJournalComponents)
		aStyles = m_pJournalComponents->GetStyleNames();
	for(std::vector<std::tstring>::iterator it = aStyles.begin(); it != aStyles.end(); it++)
		aParameters.push_back(Parameter(COMBO_STRING, *it));
	aMapParameters.insert(aMapParameters.end(), std::make_pair(COMBO_STYLE_ID, aParameters));

	//static_header2
	aParameters.push_back(Parameter(TYPE, HEADER));
	aMapParameters.insert(aMapParameters.end(), std::make_pair(STATIC_HEADER2_ID, aParameters));

	//add_typeh
	aParameters.clear();
	aParameters.push_back(Parameter(DEPENDENTS, ADD_TYPEH_ID));
	aParameters.push_back(Parameter(DEPENDENTS, LIST_TYPEH_ID));
	aParameters.push_back(Parameter(DEPENDENTS, EDIT_TYPEH_ID));
	aParameters.push_back(Parameter(DEPENDENTS, DELETE_TYPEH_ID));
	aMapParameters.insert(aMapParameters.end(), std::make_pair(ADD_TYPEH_ID, aParameters));

	//delete_typeh
	aParameters.clear();
	aParameters.push_back(Parameter(DEPENDENTS, LIST_TYPEH_ID));
	aParameters.push_back(Parameter(DEPENDENTS, DELETE_TYPEH_ID));
	aMapParameters.insert(aMapParameters.end(), std::make_pair(DELETE_TYPEH_ID, aParameters));

	//edit_typeh
	aParameters.clear();
	aParameters.push_back(Parameter(DEPENDENTS, ADD_TYPEH_ID));
	aMapParameters.insert(aMapParameters.end(), std::make_pair(EDIT_TYPEH_ID, aParameters));

	//add_typef
	aParameters.clear();
	aParameters.push_back(Parameter(DEPENDENTS, ADD_TYPEF_ID));
	aParameters.push_back(Parameter(DEPENDENTS, LIST_TYPEF_ID));
	aParameters.push_back(Parameter(DEPENDENTS, EDIT_TYPEF_ID));
	aParameters.push_back(Parameter(DEPENDENTS, DELETE_TYPEF_ID));
	aMapParameters.insert(aMapParameters.end(), std::make_pair(ADD_TYPEF_ID, aParameters));

	//delete_typef
	aParameters.clear();
	aParameters.push_back(Parameter(DEPENDENTS, LIST_TYPEF_ID));
	aParameters.push_back(Parameter(DEPENDENTS, DELETE_TYPEF_ID));
	aMapParameters.insert(aMapParameters.end(), std::make_pair(DELETE_TYPEF_ID, aParameters));

	//edit_typef
	aParameters.clear();
	aParameters.push_back(Parameter(DEPENDENTS, ADD_TYPEF_ID));
	aMapParameters.insert(aMapParameters.end(), std::make_pair(EDIT_TYPEF_ID, aParameters));

	//list_typh
	aParameters.clear();
	aParameters.push_back(Parameter(DEPENDENTS, ADD_TYPEH_ID));
	aParameters.push_back(Parameter(DEPENDENTS, DELETE_TYPEH_ID));
	std::map<int, std::tstring> aTypeH = IJournalComponent::GetHousingManager()->GetType(Housing::HOUSING);
	for (std::map<int, std::tstring>::iterator it = aTypeH.begin(); it != aTypeH.end(); it++){
		m_aTypeHousing.insert(std::make_pair(it->second, it->first + 1));
		aParameters.push_back(Parameter(LIST_STRING, it->second + _T("|") + CConvertFunction::ConvertFromInt(it->first + 1)));
	}
	aMapParameters.insert(aMapParameters.end(), std::make_pair(LIST_TYPEH_ID, aParameters));

	//list_typef
	aParameters.clear();
	aParameters.push_back(Parameter(DEPENDENTS, ADD_TYPEF_ID));
	aParameters.push_back(Parameter(DEPENDENTS, DELETE_TYPEF_ID));
	std::map<int, std::tstring> aTypeF = IJournalComponent::GetHousingManager()->GetType(Housing::FRAME);
	for (std::map<int, std::tstring>::iterator it = aTypeF.begin(); it != aTypeF.end(); it++){
		m_aTypeFrame.insert(std::make_pair(it->second, it->first + 1));
		aParameters.push_back(Parameter(LIST_STRING, it->second + _T("|") + CConvertFunction::ConvertFromInt(it->first + 1)));
	}
	aMapParameters.insert(aMapParameters.end(), std::make_pair(LIST_TYPEF_ID, aParameters));
	return aMapParameters;
}

std::map<std::tstring, std::tstring> CJournalSettings::GetDefaultStates(){
	std::map<std::tstring, std::tstring> aDefault;
	std::vector<std::tstring> styleNames;
	if(m_pJournalComponents)
		styleNames = m_pJournalComponents->GetStyleNames();
	if(!styleNames.empty())
		aDefault.insert(aDefault.end(), std::make_pair(COMBO_STYLE_ID, styleNames.at(0)));
	return aDefault;
}

std::map<std::tstring, std::tstring> CJournalSettings::GetStates(){
	std::map<std::tstring, std::tstring> aStates;
	aStates.insert(aStates.end(), std::make_pair(STATIC_HEADER_ID, ILanguageManager::GetLanguageManager()->GetString(SETTINGS_ID)));
	aStates.insert(aStates.end(), std::make_pair(STATIC_STYLE_ID, ILanguageManager::GetLanguageManager()->GetString(STYLE_JOURNAL_ID)));
	aStates.insert(aStates.end(), std::make_pair(COMBO_STYLE_ID, ILanguageManager::GetLanguageManager()->GetString(_ttoi(CCommonFunction::GetRegValue(JOURNAL_REG_NAME, COMBO_STYLE_ID).c_str()))));
	aStates.insert(aStates.end(), std::make_pair(STATIC_HEADER2_ID, ILanguageManager::GetLanguageManager()->GetString(USER_SETTINGS_ID)));
	aStates.insert(aStates.end(), std::make_pair(STATIC_TYPEH_ID, ILanguageManager::GetLanguageManager()->GetString(TYPE_HOUSING_ID)));
	aStates.insert(aStates.end(), std::make_pair(STATIC_TYPEF_ID, ILanguageManager::GetLanguageManager()->GetString(TYPE_FRAME_ID)));
	aStates.insert(aStates.end(), std::make_pair(ADD_TYPEH_ID, BUTTON_STATE_3));
	aStates.insert(aStates.end(), std::make_pair(ADD_TYPEF_ID, BUTTON_STATE_3));
	aStates.insert(aStates.end(), std::make_pair(DELETE_TYPEH_ID, BUTTON_STATE_3));
	aStates.insert(aStates.end(), std::make_pair(DELETE_TYPEF_ID, BUTTON_STATE_3));
//	aStates.insert(aStates.end(), std::make_pair(UPDATE_TYPEH_ID, BUTTON_STATE_3));
//	aStates.insert(aStates.end(), std::make_pair(UPDATE_TYPEF_ID, BUTTON_STATE_3));
	return aStates;
}

std::vector<Window> CJournalSettings::GetWindows(){
	std::vector<Window> aWindows;
	aWindows.push_back(Window(STATIC, 10, 0, 415, 23, SS_CENTER, ILanguageManager::GetLanguageManager()->GetString(SETTINGS_ID), STATIC_HEADER_ID));
	aWindows.push_back(Window(STATIC, 10, 30, 415, 15, SS_LEFT, ILanguageManager::GetLanguageManager()->GetString(STYLE_JOURNAL_ID), STATIC_STYLE_ID));
	aWindows.push_back(Window(COMBO_BOX, 10, 45, 415, 20, CBS_DROPDOWNLIST, _T(""), COMBO_STYLE_ID));

	aWindows.push_back(Window(STATIC, 10, 80, 415, 23, SS_CENTER, ILanguageManager::GetLanguageManager()->GetString(USER_COLUMN_ID), STATIC_HEADER2_ID));
	aWindows.push_back(Window(STATIC, 10, 110, 415, 15, SS_LEFT, ILanguageManager::GetLanguageManager()->GetString(TYPE_HOUSING_ID), STATIC_TYPEH_ID));
	aWindows.push_back(Window(EDIT, 10, 125, 330, 20, WS_BORDER | ES_LEFT, _T(""), EDIT_TYPEH_ID));
	aWindows.push_back(Window(LIST_BOX, 10, 150, 330, 120, WS_BORDER | WS_VSCROLL, _T(""), LIST_TYPEH_ID));
	aWindows.push_back(Window(BUTTON_ELEMENT, 345, 123, 80, 25, BS_CENTER, ILanguageManager::GetLanguageManager()->GetString(ADD_ID), ADD_TYPEH_ID));
	aWindows.push_back(Window(BUTTON_ELEMENT, 345, 150, 80, 25, BS_CENTER, ILanguageManager::GetLanguageManager()->GetString(DELETE_ID), DELETE_TYPEH_ID));
//	aWindows.push_back(Window(BUTTON_ELEMENT, 345, 177, 80, 25, BS_CENTER, ILanguageManager::GetLanguageManager()->GetString(CHANGE_ID), UPDATE_TYPEH_ID));

	aWindows.push_back(Window(STATIC, 10, 270, 415, 15, SS_LEFT, ILanguageManager::GetLanguageManager()->GetString(TYPE_FRAME_ID), STATIC_TYPEF_ID));
	aWindows.push_back(Window(EDIT, 10, 285, 330, 20, WS_BORDER | ES_LEFT, _T(""), EDIT_TYPEF_ID));
	aWindows.push_back(Window(LIST_BOX, 10, 310, 330, 120, WS_BORDER | WS_VSCROLL, _T(""), LIST_TYPEF_ID));
	aWindows.push_back(Window(BUTTON_ELEMENT, 345, 283, 80, 25, BS_CENTER, ILanguageManager::GetLanguageManager()->GetString(ADD_ID), ADD_TYPEF_ID));
	aWindows.push_back(Window(BUTTON_ELEMENT, 345, 310, 80, 25, BS_CENTER, ILanguageManager::GetLanguageManager()->GetString(DELETE_ID), DELETE_TYPEF_ID));
//	aWindows.push_back(Window(BUTTON_ELEMENT, 345, 337, 80, 25, BS_CENTER, ILanguageManager::GetLanguageManager()->GetString(CHANGE_ID), UPDATE_TYPEF_ID));
	return aWindows;
}

void CJournalSettings::SetState(const std::tstring& strId, const std::tstring& strState){
	__super::SetState(strId, strState);
	if(strId == LIST_TYPEF_ID){
		CListBoxStateParser stateParser(strState);
		int nId = m_aTypeFrame[stateParser.GetCurSel().c_str()];
		if(stateParser.GetCurSel().empty() || nId <= 7 && nId > 0)
			m_aChange[DELETE_TYPEF_ID] = BUTTON_STATE_3;
		else
			m_aChange[DELETE_TYPEF_ID] = BUTTON_STATE_2;
	}else if(strId == LIST_TYPEH_ID){
			CListBoxStateParser stateParser(strState);
			int nId = m_aTypeHousing[stateParser.GetCurSel().c_str()];
			if(stateParser.GetCurSel().empty() || nId <= 7 && nId > 0)
				m_aChange[DELETE_TYPEH_ID] = BUTTON_STATE_3;
			else
				m_aChange[DELETE_TYPEH_ID] = BUTTON_STATE_2;
	}else if(strId == ADD_TYPEH_ID && m_aChange[ADD_TYPEH_ID] != BUTTON_STATE_1){
		CListBoxStateParser stateParser(GetState(LIST_TYPEH_ID));
		stateParser.AddChange(GetState(EDIT_TYPEH_ID), 0, _ADD);
		stateParser.SetCurSel(GetState(EDIT_TYPEH_ID));
		m_aChange[LIST_TYPEH_ID] = stateParser.GetState();
		m_aChange[DELETE_TYPEH_ID] = BUTTON_STATE_2;
		m_aChange[ADD_TYPEH_ID] = BUTTON_STATE_3;
		m_aChange[EDIT_TYPEH_ID] = _T("");
	}else if(strId == DELETE_TYPEH_ID && m_aChange[DELETE_TYPEH_ID] != BUTTON_STATE_1){
		CListBoxStateParser stateParser(m_aChange[LIST_TYPEH_ID].c_str());
		stateParser.AddChange(stateParser.GetCurSel(), m_aTypeHousing[stateParser.GetCurSel().c_str()], _DELETE);
		stateParser.SetCurSel(_T(""));
		m_aChange[LIST_TYPEH_ID] = stateParser.GetState();
		m_aChange[DELETE_TYPEH_ID] = BUTTON_STATE_3;
	}else if(strId == EDIT_TYPEH_ID){
		if(strState.empty())
			m_aChange[ADD_TYPEH_ID] = BUTTON_STATE_3;
		else
			m_aChange[ADD_TYPEH_ID] = BUTTON_STATE_2;
	}else if(strId == ADD_TYPEF_ID && m_aChange[ADD_TYPEF_ID] != BUTTON_STATE_1){
		CListBoxStateParser stateParser(GetState(LIST_TYPEF_ID));
		stateParser.AddChange(GetState(EDIT_TYPEF_ID), 0, _ADD);
		stateParser.SetCurSel(GetState(EDIT_TYPEF_ID));
		m_aChange[LIST_TYPEF_ID] = stateParser.GetState();
		m_aChange[DELETE_TYPEF_ID] = BUTTON_STATE_2;
		m_aChange[ADD_TYPEF_ID] = BUTTON_STATE_3;
		m_aChange[EDIT_TYPEF_ID] = _T("");
	}else if(strId == DELETE_TYPEF_ID && m_aChange[DELETE_TYPEF_ID] != BUTTON_STATE_1){
		CListBoxStateParser stateParser(m_aChange[LIST_TYPEF_ID].c_str());
		stateParser.AddChange(stateParser.GetCurSel(), m_aTypeHousing[stateParser.GetCurSel().c_str()], _DELETE);
		stateParser.SetCurSel(_T(""));
		m_aChange[LIST_TYPEF_ID] = stateParser.GetState();
		m_aChange[DELETE_TYPEF_ID] = BUTTON_STATE_3;
	}else if(strId == EDIT_TYPEF_ID){
		if(strState.empty())
			m_aChange[ADD_TYPEF_ID] = BUTTON_STATE_3;
		else
			m_aChange[ADD_TYPEF_ID] = BUTTON_STATE_2;
	}
}

std::tstring CJournalSettings::GetSaveValue(const std::tstring& strValueName, const std::tstring& strValue, bool& bSave){
	if(strValueName == COMBO_STYLE_ID){
		int nId;
		TCHAR value[10];
		bSave = TRUE;
		nId = m_pJournalComponents->GetView(strValue)->GetStyleView()->GetStyleId();
		_itot_s(nId, value, 10, 10);
		return value;
	}
	return __super::GetSaveValue(strValueName, strValue, bSave);
}

bool CJournalSettings::IsChanged(){
	for (std::map<std::tstring, std::tstring>::iterator it = m_aChange.begin(); it != m_aChange.end(); it++){
		if(it->first == LIST_TYPEF_ID || it->first == LIST_TYPEH_ID){
			CListBoxStateParser stateParser(it->second);
			if(stateParser.GetAddColumn().empty() && stateParser.GetDeleteColumn().empty())
				continue;
		}
		if(it->first == DELETE_TYPEF_ID || it->first == ADD_TYPEF_ID ||
			it->first == DELETE_TYPEH_ID || it->first == ADD_TYPEH_ID ||
			it->first == EDIT_TYPEH_ID || it->first == EDIT_TYPEF_ID)
			continue;
		return true;
	}
	return false;
}

void CJournalSettings::SaveSetting(){
	std::map<std::tstring, std::tstring>::iterator it;

	if((it = m_aChange.find(LIST_TYPEH_ID)) != m_aChange.end()){
		std::tstring strState = SaveTypeList(Housing::HOUSING, it->second);
		m_aChange.erase(it);
		for(std::map<sWindow, std::tstring>::iterator it1 = m_aWindows.begin(); it1 != m_aWindows.end(); it1++){
			if(it1->first.m_window.m_strId == LIST_TYPEH_ID)
				((sWindow&)it1->first).m_strState = strState;
		}
	}
	if((it = m_aChange.find(LIST_TYPEF_ID)) != m_aChange.end()){
		std::tstring strState = SaveTypeList(Housing::HOUSING, it->second);
		m_aChange.erase(it);
		for(std::map<sWindow, std::tstring>::iterator it1 = m_aWindows.begin(); it1 != m_aWindows.end(); it1++){
			if(it1->first.m_window.m_strId == LIST_TYPEF_ID)
				((sWindow&)it1->first).m_strState = strState;
		}
	}
	if((it = m_aChange.find(ADD_TYPEH_ID)) != m_aChange.end())
		m_aChange.erase(it);
	if((it = m_aChange.find(ADD_TYPEF_ID)) != m_aChange.end())
		m_aChange.erase(it);
	if((it = m_aChange.find(EDIT_TYPEF_ID)) != m_aChange.end())
		m_aChange.erase(it);
	if((it = m_aChange.find(EDIT_TYPEF_ID)) != m_aChange.end())
		m_aChange.erase(it);
	if((it = m_aChange.find(DELETE_TYPEH_ID)) != m_aChange.end()){
		for (std::map<sWindow, std::tstring>::iterator it1 = m_aWindows.begin(); it1 != m_aWindows.end(); it1++){
			if(it1->first.m_window.m_strId == it->first){
				it1->second = it->second;
				break;
			}
		}
		m_aChange.erase(it);
	}
	if((it = m_aChange.find(DELETE_TYPEF_ID)) != m_aChange.end()){
		for (std::map<sWindow, std::tstring>::iterator it1 = m_aWindows.begin(); it1 != m_aWindows.end(); it1++){
			if(it1->first.m_window.m_strId == it->first){
				it1->second = it->second;
				break;
			}
		}
		m_aChange.erase(it);
	}
	__super::SaveSetting();
}

void CJournalSettings::PostSave(){
//	m_pApp->GetChangeManager()->SetChange(SETTINGS, CHANGE_JOURNAL_SETTINGS);
}

std::tstring CJournalSettings::SaveTypeList(Housing::change_type tp, const std::tstring& strState){
	CListBoxStateParser stateParser(strState);
	std::vector<Col> aAddColumn  = stateParser.GetAddColumn();
	std::vector<Col> aDelColumn  = stateParser.GetDeleteColumn();
	for(std::vector<Col>::iterator it1 = aDelColumn.begin(); it1 != aDelColumn.end(); it1++)
		IJournalComponent::GetHousingManager()->DeleteType(tp, it1->m_nId - 1);
	for(std::vector<Col>::iterator it1 = aAddColumn.begin(); it1 != aAddColumn.end(); it1++)
		IJournalComponent::GetHousingManager()->AddType(tp, it1->m_strName);
	stateParser.SaveChange();
	return stateParser.GetState();
}